/*
 * @Description: UserService
 */
const UserModel = require("../../models/UserModel")

const UserService = {
  login: async ({ username, password }) => {
    return UserModel.find({ username, password })
  },
  upload: async ({ username, _id, sex, avatar }) => {
    // 有无更换图片
    if (avatar) {
      return UserModel.updateOne({ _id }, {
        username, sex, avatar
      })
    } else {
      return UserModel.updateOne({ _id }, {
        username, sex
      })
    }

  },
  add: async ({ username, password, sex, avatar, role }) => {
    return UserModel.create({ username, password, sex, avatar, role })
  },
  getList: async (id) => {
    return id ? UserModel.find({ _id: id }, ['username', 'sex', 'avatar', 'role']) : UserModel.find({}, ['username', 'sex', 'avatar', 'role'])
  },
  delOne: async ({ _id }) => {
    return UserModel.deleteOne({ _id })
  },
  updateOne: async (body) => {
    return UserModel.updateOne({ _id: body._id, }, body)
  },
}
module.exports = UserService
